//引入模板引擎
import userListPageTpl from "../views/user-pages.art";
//引入公共事件模块
import page from "../dataBus/page.js";
//分页事件监听
const pagination=(data,pageSize,currentPage)=>{
    let total=data.length;
    let sizes=Math.ceil(total/pageSize);
    // console.log(currentPage,sizes,total%pageSize);
    if(total%pageSize===0 && currentPage==sizes+1 && sizes!=0){//当最后一页无数据时并且处于最后一页时进入
        page.setCurrentPage(--currentPage);
        $("body").trigger("changePage",page.currentPage);
    };
    $("#user-pages").html(userListPageTpl({
        sizes
    }));
    //初始化高亮当前页码
    $("#user-pages-list li").eq(currentPage).addClass("active");
    //点击页面高亮点击页码并且渲染页面
    $("#user-pages-list li:not(:first-child,:last-child)").on("click",function(){
        $(this).addClass("active").siblings().removeClass("active");
        page.setCurrentPage($(this).index());
        $("body").trigger("changePage",$(this).index());
        // currentPage=$(this).index();
        // console.log("当前页:",currentPage);
    });
    //点击上一页下一页按钮事件监听
    if(currentPage===1){
        $("#user-pages-list li:first-child").addClass("disabled");
    }else{
        $("#user-pages-list li:first-child").removeClass("disabled");
        $("#user-pages-list li:first-child").on("click",function(){
            page.setCurrentPage(--currentPage);
            $("body").trigger("changePage",page.currentPage);
        });
    };
    if(currentPage===sizes || sizes===0){//当初于最后一页或者无数据时进入
        $("#user-pages-list li:last-child").addClass("disabled");
    }else{
        $("#user-pages-list li:last-child").removeClass("disabled");
        $("#user-pages-list li:last-child").on("click",function(){
            page.setCurrentPage(++currentPage);
            $("body").trigger("changePage",page.currentPage);
        });
    };
};

export default pagination